package tech.abing.spider.task.pageprocessor.amac;

import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.FundManagerGuwenEntity;
import tech.abing.spider.util.UuIdPublic;

public class ManagerGuwenProcessor implements PageProcessor {
	
	static Pattern normalContain = Pattern.compile("totalPages");//正常页面必定会存在的字符，你也可以使用页面上其他的字符
	private static Logger logger = Logger.getLogger(ManagerGuwenProcessor.class);
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue,
			List<Proccessable> objectContainer) throws Exception {
		
		JSONObject jsonObj = JSONObject.fromObject(page.getContent());
		
		boolean first = jsonObj.getBoolean("first");
		if(first){
			//总页数
			long totalPages = jsonObj.getLong("totalPages");
			PageRequest request = page.getRequest();
			for(int i = 1; i < totalPages; i++){
				String url = request.getUrl().replace("&page=0&", "&page="+i+"&");
				PageRequest req = context.createPageRequest(url, ManagerGuwenProcessor.class);
				
				req.setMethod(request.getMethod());
				req.setPostType(request.getPostType());
				req.setReqPayloadParams(request.getReqPayloadParams());
				
				req.setHeaders(request.getHeaders());
				
				req.setRequestParams(request.getRequestParams());
				
				queue.add(req);
				
			}
		}
		
		logger.info("分页请求:"+page.getRequest().getUrl());
		String pageUrl = page.getRequest().getUrl();
		String pageIndex = pageUrl.substring(pageUrl.indexOf("page=")+5,pageUrl.lastIndexOf("&"));
		
		//解析数据
		JSONArray content = jsonObj.getJSONArray("content");
		String type = page.getRequest().getRequestParams().get("level").toString();
		if(content != null && content.size() > 0){
			for(int i = 0; i < content.size(); i++){
				JSONObject ctxObj = content.getJSONObject(i);
				String managerName = ctxObj.getString("managerName");
				FundManagerGuwenEntity guwen = new FundManagerGuwenEntity();
				guwen.setId(UuIdPublic.timeStrongID(5));
				guwen.setManagerName(managerName);
				guwen.setScaleLevel(Integer.valueOf(type));
				guwen.setScaleDesc(getScaleDesc(guwen.getScaleLevel()));
				objectContainer.add(guwen);
			}
		}
	}

	//1:0-1亿 2:1-10亿 3:10-20亿 4:20-50亿 5:50亿以上
	private String getScaleDesc(int type) {
		String result = "";
		switch (type) {
		case 1:
			result = "0-1亿";
			break;
		case 2:
			result = "1-10亿";
			break;
		case 3:
			result = "10-20亿";
			break;
		case 4:
			result = "20-50亿";
			break;
		case 5:
			result = "50亿以上";
			break;

		default:
			break;
		}
		
		return result;
	}
	
	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
